package com.forenow.demo.demo7;

import java.util.Scanner;

public class EmployeeDAOImpl implements EmployeeDAO {
    //声明一个数组容器
    private Employee[]  arr;
    static int count;
    Scanner s=new Scanner(System.in);


    public EmployeeDAOImpl() {
        //利用构造器初始化数组
        arr=new Employee[20];
    }

    @Override
    public void add(Employee employee) {
        //1.判断参数employee是否为空，如果不为空
        //2.如果count<3
        //3.要将employee添加到数组容器中,count+1
        //4.输出信息
        if (employee != null) {
            arr[count++]=employee;
        }

    }

    @Override
    public void delete(Employee employee) {

    }

    @Override
    public void updata(Employee employee) {
        //1.可以先根据id查询要修改的对象
        //2.然后将查询到的对象传递给参数进行修改
        employee.setId(2);
        employee.setName("李四");
        employee.setSal(1546);
        employee.setDept("人事部");
    }




    @Override
    public Employee[] show() {
        //1.要对employee数组进行遍历，每遍历一次就要将原数组中员工对象保存到新的数组里
         return arr;
    }

    @Override
    public Employee getQueryById(int id) {
        //1.对employee数组进行遍历
        //2.每遍历一次就要获取到一个Employee对象，判断Employee对象的id是否与参数的id是否相等
        //3.最后将查询到的对象返回
        Employee e=null;
        for (int i = 0; i <count ; i++) {
                if (id==arr[i].getId()) {
                    e=arr[i];
                }
        }
            return e;
    }

    @Override
    public void addEmp() {
        System.out.println("输入员工id：");
        int id=s.nextInt();
        System.out.println("输入员工姓名：");
        String name=s.next();
        System.out.println("输入员工工资：");
        int sal=s.nextInt();
        System.out.println("输入员工部门：");
        String dept=s.next();
        Employee empupdata=new Employee(id,name,sal,dept);
        this.add(empupdata);
        Employee[] e=this.show();
        for (int i = 0; i <count; i++) {
            System.out.println(e[i].getId() + "\t" + e[i].getName() + "\t" + e[i].getSal() + "\t" + e[i].getDept());
        }
    }

    public static void main(String[] args) {
        int flag=0;
        Scanner s=new Scanner(System.in);
        System.out.println("输入员工id：");
        int id=s.nextInt();
        System.out.println("输入员工姓名：");
        String name=s.next();
        System.out.println("输入员工工资：");
        int sal=s.nextInt();
        System.out.println("输入员工部门：");
        String dept=s.next();
        Employee employee1=new Employee(1,"cindy",1500,"销售");
        Employee employee2=new Employee(2,"Bob\t",1456,"经理");
        Employee employee3=new Employee(3,"kiity",1567,"经理");
        Employee employee4=new Employee(4,"tom\t",1660,"销售");
        Employee employee=new Employee(id,name,sal,dept);
        EmployeeDAO employeeDAO=new EmployeeDAOImpl();
        employeeDAO.add(employee1);
        employeeDAO.add(employee2);
        employeeDAO.add(employee3);
        employeeDAO.add(employee4);
        employeeDAO.add(employee);
        Employee[] e=employeeDAO.show();
        for (int i = 0; i <count; i++) {
            System.out.println(e[i].getId() + "\t" + e[i].getName() + "\t" + e[i].getSal() + "\t" + e[i].getDept());
        }
        System.out.println("输入想要查询员工信息的ID：");
        int EmpId=s.nextInt();
        Employee emp=employeeDAO.getQueryById(EmpId);
        if (emp != null) {
            System.out.println(emp.getId() + "\t" + emp.getName() + "\t" + emp.getSal() + "\t" + emp.getDept());
        }else {
            System.out.println("error");
        }
        do {
            System.out.println("是否添加员工信息（y/n）：");
            String ifgo=s.next();
            if (ifgo.equals("y")) {
                for (int i = 0; i <EmpId; i++) {
                    employeeDAO.addEmp();
                }
            }else {
                System.exit(0);
            }
            flag++;
        }while (flag<5);
        employeeDAO.updata(emp);


    }
}
